//
//  RWOrderListTableViewCell.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/27.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWOrderListTableViewCell.h"
#import "RWMyOrderModel.h"
#import "RWGoodsDetailModel.h"

#define kOrderListButtonBaseTag 580

@interface RWOrderListTableViewCell ()

@property (nonatomic, copy) RWMyOrderModel *myOrderModel;
@property (nonatomic, copy) RWGoodsDetailInfoModel *goodsModel;

@end

@implementation RWOrderListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setOrderListCell];
    }
    return self;
}

- (void)loadDataWithModel:(id)infoModel{
    
    _myOrderModel = [RWMyOrderModel mj_objectWithKeyValues:infoModel];
    _goodsModel = [RWGoodsDetailInfoModel mj_objectWithKeyValues:_myOrderModel.goods[0]];
    
    [self configDatas];
}

- (void)configDatas{
    
    NSInteger orderState = [_myOrderModel.order_state integerValue];
    
    switch (orderState) {
        case 0:
            _stateIcon.text = @"已取消";
            //取消按钮
            _cancelOrderBtn.hidden = YES;
            //去付款按钮
            _toPayBtn.hidden = YES;
            //确认收货
            _recieveBtn.hidden = YES;
            //[_cancelOrderBtn removeFromSuperview];
            //[_toPayBtn removeFromSuperview];
            break;
        case 10:{
            _stateIcon.text = @"未付款";
            //_detailBtn.frame = CGRectMake(FTY_SCREEN_WIDTH - 90.*FTY_WIDTH_BASE, CGRectGetMaxY(self.frame)-40.*FTY_HEIGHT_BASE, 80.*FTY_WIDTH_BASE, 30.*FTY_HEIGHT_BASE);
            //取消按钮
            _cancelOrderBtn.hidden = NO;
            //去付款按钮
            _toPayBtn.hidden = NO;
            //确认收货
            _recieveBtn.hidden = YES;
            break;
        }
        case 20:
            _stateIcon.text = @"已付款";
            //取消按钮
            _cancelOrderBtn.hidden = NO;
            //去付款按钮
            _toPayBtn.hidden = YES;
            //确认收货
            _recieveBtn.hidden = YES;
            break;
        case 30:
            _stateIcon.text = @"已发货";
            //取消按钮
            _cancelOrderBtn.hidden = YES;
            //去付款按钮
            _toPayBtn.hidden = YES;
            //确认收货
            _recieveBtn.hidden = NO;
            break;
        case 40:
            _stateIcon.text = @"已收货";
            //取消按钮
            _cancelOrderBtn.hidden = YES;
            //去付款按钮
            _toPayBtn.hidden = YES;
            //确认收货
            _recieveBtn.hidden = YES;
            break;
            
        default:
            break;
    }
    
    _idLab.text = _myOrderModel.order_sn;
    _timeLab.text = _myOrderModel.add_time;
    [_imageV sd_setImageWithURL:[NSURL URLWithString:_myOrderModel.goods[0][@"goods_image"]] placeholderImage:[UIImage imageNamed:@"huibangke"]];
    _titleLab.text = _goodsModel.goods_name;
    _moneyLab.text = _goodsModel.goods_pay_price;
    _sumLab.text = [NSString stringWithFormat:@"共1件商品 总计：¥ %@ (含运费¥0.00)", _myOrderModel.sum];
}

- (void)setOrderListCell{

    //第一行
    _stateIcon = [[UILabel alloc] init];
    [self.contentView addSubview:_stateIcon];
    _stateIcon.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
    _stateIcon.layer.masksToBounds = YES;
    _stateIcon.layer.cornerRadius = 5.*FTY_WIDTH_BASE;
    _stateIcon.text = @"已付款";
    _stateIcon.textAlignment= NSTextAlignmentCenter;
    _stateIcon.font = FTY_FONTTSIZE(14.);
    _stateIcon.textColor = [FTYColor whiteColor];
    
    _idLab = [[UILabel alloc] init];
    [self.contentView addSubview:_idLab];
    _idLab.textColor = [FTYColor blackColor];
    _idLab.font = FTY_FONTTSIZE(14);
    [_idLab sizeToFit];
    
    _timeLab = [[UILabel alloc] init];
    [self.contentView addSubview:_timeLab];
    _timeLab.textColor = [FTYColor blackColor];
    _timeLab.font = FTY_FONTTSIZE(12);
    [_timeLab sizeToFit];
    
    //第二行
    _imageV = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageV];
    
    _titleLab = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLab];
    _titleLab.numberOfLines = 2;
    _titleLab.textColor = [FTYColor blackColor];
    _titleLab.font = FTY_FONTTSIZE(15.*FTY_WIDTH_BASE);
    
    _moneyLab = [[UILabel alloc] init];
    [self.contentView addSubview:_moneyLab];
    _moneyLab.textColor = [FTYColor getMainColorWithAlpha:1.];
    _moneyLab.font = FTY_FONTTSIZE(15.*FTY_WIDTH_BASE);
    
    _numberLab = [[UILabel alloc] init];
    [self.contentView addSubview:_numberLab];
    _numberLab.text = @"*1";
    _numberLab.textColor = [FTYColor getMainColorWithAlpha:1.];
    _numberLab.font = FTY_FONTTSIZE(15.*FTY_WIDTH_BASE);
    
    //第三行
    _line1 = [[UILabel alloc] init];
    [self.contentView addSubview:_line1];
    _line1.backgroundColor = [FTYColor grayColor];
    
    _sumLab = [[UILabel alloc] init];
    [self.contentView addSubview:_sumLab];
    _sumLab.textColor = [FTYColor blackColor];
    _sumLab.font = FTY_FONTTSIZE(12);
    
    _line2 = [[UILabel alloc] init];
    [self.contentView addSubview:_line2];
    _line2.backgroundColor = [FTYColor grayColor];
    
    _detailBtn = [[UIButton alloc] init];
    [self.contentView addSubview:_detailBtn];
    _detailBtn.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
//    _detailBtn.layer.masksToBounds = YES;
//    _detailBtn.layer.borderColor = [FTYColor getEEEEEEColorWithAlpha:1.].CGColor;
//    _detailBtn.layer.borderWidth = .5*FTY_WIDTH_BASE;
    [_detailBtn setTitle:@"订单详情" forState:UIControlStateNormal];
    [_detailBtn setTitleColor:[FTYColor blackColor] forState:UIControlStateNormal];
    _detailBtn.titleLabel.font = FTY_FONTTSIZE(14.);
    _detailBtn.tag = kOrderListButtonBaseTag;
    [_detailBtn addTarget:self action:@selector(orderListBtnAction:) forControlEvents:UIControlEventTouchUpInside];
   // _detailBtn.hidden = YES;
    
    _cancelOrderBtn = [[UIButton alloc] init];
    [self.contentView addSubview:_cancelOrderBtn];
    _cancelOrderBtn.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [_cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
    [_cancelOrderBtn setTitleColor:[FTYColor blackColor] forState:UIControlStateNormal];
    _cancelOrderBtn.titleLabel.font = FTY_FONTTSIZE(14.);
    _cancelOrderBtn.tag = kOrderListButtonBaseTag + 1;
    [_cancelOrderBtn addTarget:self action:@selector(orderListBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //_cancelOrderBtn.hidden = YES;
    
    _toPayBtn = [[UIButton alloc] init];
    [self.contentView addSubview:_toPayBtn];
    _toPayBtn.layer.masksToBounds = YES;
    _toPayBtn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
    [_toPayBtn setTitle:@"去付款" forState:UIControlStateNormal];
    [_toPayBtn setTitleColor:[FTYColor whiteColor] forState:UIControlStateNormal];
    _toPayBtn.titleLabel.font = FTY_FONTTSIZE(14.);
    _toPayBtn.tag = kOrderListButtonBaseTag + 2;
    [_toPayBtn addTarget:self action:@selector(orderListBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //_toPayBtn.hidden = YES;
    
    _recieveBtn = [[UIButton alloc] init];
    [self.contentView addSubview:_recieveBtn];
    _recieveBtn.layer.masksToBounds = YES;
    _recieveBtn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
    [_recieveBtn setTitle:@"确认收货" forState:UIControlStateNormal];
    [_recieveBtn setTitleColor:[FTYColor whiteColor] forState:UIControlStateNormal];
    _recieveBtn.titleLabel.font = FTY_FONTTSIZE(14.);
    _recieveBtn.tag = kOrderListButtonBaseTag + 3;
    [_recieveBtn addTarget:self action:@selector(orderListBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _bottomLine = [[UIView alloc] init];
    [self.contentView addSubview:_bottomLine];
    _bottomLine.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    
}

- (void)orderListBtnAction:(UIButton *)btn{

    if (_delegate && [_delegate respondsToSelector:@selector(orderListAction:orderSN:)]) {
        [_delegate orderListAction:btn orderSN:_myOrderModel.order_sn];
    }
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    [_stateIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(8.*FTY_HEIGHT_BASE);
        make.width.mas_equalTo(60.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(20.*FTY_HEIGHT_BASE);
    }];
    
    [_idLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_stateIcon.mas_right).offset(8.*FTY_WIDTH_BASE);
    }];
    
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_idLab.mas_top);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    
    [_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_stateIcon.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_stateIcon.mas_left);
        make.width.height.mas_equalTo(100.*FTY_WIDTH_BASE);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_stateIcon.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_imageV.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    
    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_titleLab.mas_left);
    }];
    
    [_numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_moneyLab.mas_top);
        make.right.mas_equalTo(_titleLab.mas_right);
    }];
    
    [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageV.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    [_sumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line1.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(_titleLab.mas_right);
    }];
    
    [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_sumLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    /*
    [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(-100.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
    }];
    
    [_cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_detailBtn.mas_top);
        make.right.mas_equalTo(_detailBtn.mas_left).offset(-10.*FTY_WIDTH_BASE);
        make.width.height.mas_equalTo(_detailBtn);
    }];
    
    [_toPayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_detailBtn.mas_top);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
    }];
    
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(10.*FTY_HEIGHT_BASE);
    }];
     */
    
    /*
            取消  详情  付款  收货 0代表隐藏   1代表显示
     未付款    1   1   1   0
     已付款    1   1   0   0
     已发货    0   1   0   1
     已收货    0   1   0   0
     已取消    0   1   0   0
     */
    //取消按钮 和 付款按钮 收货按钮 都隐藏 ，已收货，已取消
    if (_cancelOrderBtn.hidden==YES && _toPayBtn.hidden==YES && _recieveBtn.hidden==YES) {
        [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.removeExisting = YES;
            make.top.mas_equalTo(_line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];//取消按钮不隐藏，付款按钮 收货按钮隐藏 已付款
    }else if (_cancelOrderBtn.hidden==NO&& _toPayBtn.hidden==YES && _recieveBtn.hidden==YES) {
        [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //删除掉之前的约束，不然会因为cell复用出现button错位问题
            make.removeExisting = YES;
            make.top.mas_equalTo(_line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];
        
        [_cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_detailBtn.mas_top);
            make.right.mas_equalTo(_detailBtn.mas_left).offset(-10.*FTY_WIDTH_BASE);
            make.width.height.mas_equalTo(_detailBtn);
        }];
    }else if (_cancelOrderBtn.hidden==NO&& _toPayBtn.hidden==NO&&_detailBtn.hidden==NO){
        [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.removeExisting = YES;
            make.top.mas_equalTo(_line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
            make.right.mas_equalTo(-100.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];
        
        [_cancelOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_detailBtn.mas_top);
            make.right.mas_equalTo(_detailBtn.mas_left).offset(-10.*FTY_WIDTH_BASE);
            make.width.height.mas_equalTo(_detailBtn);
        }];
        
        [_toPayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_detailBtn.mas_top);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];//已发货
    }else if (_cancelOrderBtn.hidden==YES && _toPayBtn.hidden==YES && _recieveBtn.hidden==NO){
        [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            //删除掉之前的约束，不然会因为cell复用出现button错位问题
            make.removeExisting = YES;
            make.top.mas_equalTo(_line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];
        
        [_recieveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_detailBtn.mas_top);
            make.right.mas_equalTo(_detailBtn.mas_left).offset(-10.*FTY_WIDTH_BASE);
            make.width.height.mas_equalTo(_detailBtn);
        }];
    }
    
}
    
   
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
